import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:url_launcher/url_launcher.dart';

class CardetailPage extends StatefulWidget {
  // CardetailPage({Key key, this.title}) : super(key: key);

  //final String title;
  final Map param;
  CardetailPage(this.param);

  @override
  _MyHomePageState createState() => new _MyHomePageState(this.param);
}

class _MyHomePageState extends State<CardetailPage> {
  final Map param;
  _MyHomePageState(this.param);
  List formList;
  initState() {
    super.initState();
    formList = [
      {
        "title": '车牌号111111',
        'sub_title': '云A00001',
        'sub_color': Color.fromRGBO(27, 182, 251, 1)
      },
      {
        "title": '车牌号',
        'sub_title': '云A00001',
        'sub_color': Color.fromRGBO(27, 182, 251, 1)
      },
      {
        "title": '车牌号',
        'sub_title': '云A00001',
        'sub_color': Color.fromRGBO(27, 182, 251, 1)
      },
      {
        "title": '车牌号',
        'sub_title': '云A00001',
        'sub_color': Color.fromRGBO(27, 182, 251, 1)
      },
    ];
  }

  Widget buildcontent() {
    List<Widget> tiles = [];
    Widget content;
    tiles.add(flash());
    // for (var item in formList) {
    //   tiles.add(new Row(children: <Widget>[
    //     Expanded(child:
    //     new Padding(
    //         padding: new EdgeInsets.fromLTRB(10, 10, 10, 0),
    //         child: new Text(
    //           item['title'],
    //           style: TextStyle(fontSize: 16),
    //         )),flex: 2,),
    //          new Padding(
    //         padding: new EdgeInsets.fromLTRB(10, 10, 10, 0),
    //         child: new Text(
    //           item['sub_title'],
    //           style: TextStyle(fontSize: 16,color: item['sub_color']),
    //         )),
    //   ],));
    // }
    content = new Column(
        children: tiles //重点在这里，因为用编辑器写Column生成的children后面会跟一个<Widget>[]，
        //此时如果我们直接把生成的tiles放在<Widget>[]中是会报一个类型不匹配的错误，把<Widget>[]删了就可以了
        );
    return content;
  }

  //滚动图片
  Widget flash() {
    var content = Container(
        width: MediaQuery.of(context).size.width,
        height: 200.0,
        child: Swiper(
          itemBuilder: _swiperBuilder,
          itemCount: param["car_thumb"].length,
          pagination: new SwiperPagination(
              builder: DotSwiperPaginationBuilder(
            color: Colors.black54,
            activeColor: Colors.white,
          )),
          control: new SwiperControl(),
          scrollDirection: Axis.horizontal,
          autoplay: true,
          onTap: (index) => print('点击了第$index个'),
        ));

    return content;
  }

  Widget _swiperBuilder(BuildContext context, int index) {
    return (Image.network(
      param["car_thumb"][index],
      fit: BoxFit.contain,
    ));
  }

  //标题
  Widget titles() {
    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(0, 10, 0, 5),
        child: Container(
          child: Text(
            param["title"],
            style:
                TextStyle(color: Color.fromRGBO(16, 29, 55, 1), fontSize: 15),
          ),
        ));
    return content;
  }

  //发布者
  Widget distube() {
    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(0, 20, 0, 5),
        child: new Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                new Container(
                    alignment: Alignment.topLeft,
                    width: 20,
                    child:
                        new Image.asset('resource/images/zhanshi/dizhi.png')),

                new Padding(
                  padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
                  child: Text('发布者'),
                )
                // Expanded(
                //     flex: 1,
                //     child:
                //         new Image.asset('resource/images/zhanshi/dizhi.png')),
                // Expanded(
                //     flex: 9,
                //     child: new Padding(
                //       padding: new EdgeInsets.fromLTRB(30, 0, 0, 0),
                //       child: Text('发布者'),
                //     ))
              ],
            ),
            // Padding(
            //     padding: new EdgeInsets.fromLTRB(0, 15, 0, 15),
            //     child: Row(
            //       children: <Widget>[
            //         new Container(
            //             alignment: Alignment.topLeft,
            //             width: 20,
            //             child: new Text('')),
            //         new Padding(
            //           padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
            //           child: Text("暂无",
            //               style: new TextStyle(
            //                   color: Color.fromRGBO(120, 120, 120, 1),
            //                   fontSize: 13)),
            //         )
            //       ],
            //     )),
            Row(
              children: <Widget>[
                new Container(
                    alignment: Alignment.topLeft,
                    width: 20,
                    child: new Text('')),

                new Padding(
                  padding: new EdgeInsets.fromLTRB(5, 5, 0, 0),
                  child: Text(
                      param["contact_people"] + ' / ' + param["contact_mobile"],
                      style: new TextStyle(
                          color: Color.fromRGBO(120, 120, 120, 1),
                          fontSize: 13)),
                )
                // Expanded(
                //     flex: 1,
                //     child:
                //         new Image.asset('resource/images/zhanshi/dizhi.png')),
                // Expanded(
                //     flex: 9,
                //     child: new Padding(
                //       padding: new EdgeInsets.fromLTRB(30, 0, 0, 0),
                //       child: Text('发布者'),
                //     ))
              ],
            ),
          ],
        ));
    return content;
  }

  //上架时间
  Widget distubetime() {
    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(0, 15, 0, 5),
        child: new Column(
          children: <Widget>[
            Row(
              children: <Widget>[
                //     new Image.asset('resource/images/zhanshi/dizhi.png'),

                // new Padding(
                //   padding: new EdgeInsets.fromLTRB(25, 0, 0, 0),
                //   child: Text('发布者'),
                // )
                new Container(
                    alignment: Alignment.topLeft,
                    width: 20,
                    child: new Image.asset(
                        'resource/images/zhanshi/shangjia.png')),
                new Padding(
                  padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
                  child: RichText(
                    text: TextSpan(
                        text: '上架时间：',
                        style: TextStyle(fontSize: 14, color: Colors.black),
                        children: [
                          TextSpan(
                              text: param["add_time"],
                              style: TextStyle(
                                  fontSize: 13,
                                  color: Color.fromRGBO(120, 120, 120, 1)))
                        ]),
                  ),
                  //child: Text('上架时间：' + param["add_time"]),
                )
              ],
            ),
          ],
        ));
    return content;
  }

  Widget inspect() {
    return Padding(
      padding: new EdgeInsets.fromLTRB(0, 15, 0, 0),
      child: new Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          new GestureDetector(
                onTap: (){
                  //检测结果
                },
                child:
          Image.asset('resource/images/baoxiu/inspected.png',
              width: 15, height: 17)),
              new GestureDetector(
                onTap: (){
                  //检测结果
                },
                child:
          Padding(
              padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
              child: Text('检测报告',
                  style: TextStyle(
                    fontSize: 11,
                    color: Color.fromRGBO(212, 35, 122, 1),
                  )))),
          param["is_guratee"].toString() == "1"
              ? 
              new GestureDetector(
                onTap: (){
                  //查看保修
                },
                child:
              Padding(
                  padding: new EdgeInsets.fromLTRB(15, 0, 0, 0),
                  child: Image.asset('resource/images/baoxiu/guarateed.png',
                      width: 15, height: 17)))
              : Container(height: 0),
          param["is_guratee"].toString() == "1"
              ? 
              new GestureDetector(
                onTap: (){
                  //查看保修
                },
                child:
              Padding(
                  padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
                  child: Text('保修证明',
                      style: TextStyle(
                        fontSize: 11,
                        color: Color.fromRGBO(18, 150, 219, 1),
                      ))))
              : Container(height: 0)
        ],
      ),
    );
  }

  //
  Widget car_base_info() {
    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(0, 15, 0, 5),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                new Container(
                    alignment: Alignment.topLeft,
                    width: 20,
                    child: new Image.asset(
                        'resource/images/zhanshi/cheliangguanli.png')),
                new Padding(
                    padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
                    child: Text('基础信息'))
              ],
            ),
          ],
        ));
    return content;
  }

  String formatNum(double num, int postion) {
    if ((num.toString().length - num.toString().lastIndexOf(".") - 1) <
        postion) {
      //小数点后有几位小数
      return num.toStringAsFixed(postion)
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    } else {
      return num.toString()
          .substring(0, num.toString().lastIndexOf(".") + postion + 1)
          .toString();
    }
  }

  Widget car_base_info_detail() {
    List<Widget> tiles = [];
    List carinfo =
        param["inspectorItemsResult"]["otherData"]["checkData"][0]["post"];

    var km; //l里程
    var pl; //排量
    var qd; //驱动
    var fdj; //发动鸡
    var cj; //车架号
    var pf; //排放标准
    var lx; //类型
    var gc; //国产、进口
    var cp; //车牌
    for (var item in carinfo) {
      if (item['id'] == "3034") {
        lx = item['value'];
      }
      if (item['id'] == "4035") {
        qd = item['value'];
      }

      if (item['id'] == "4034") {
        gc = item['value'];
      }

      if (item['id'] == "4033") {
        pf = item['value'];
      }

      if (item['id'] == "4032") {
        fdj = item['value'];
      }
      if (item['id'] == "4031") {
        pl = item['value'];
      }

      if (item['id'] == "4030") {
        double temp = double.parse(item['value']) / 10000;
        km = formatNum(temp, 2) + '万';
      }

      if (item['id'] == "3030") {
        cj = item['value'];
      }

      if (item['id'] == "3029") {
        cp = item['value'];
      }
    }

    Color baseColor = Color.fromRGBO(150, 150, 150, 1);
    //var item = carinfo[i];
    tiles.add(Padding(
        padding: new EdgeInsets.fromLTRB(5, 5, 30, 0),
        child: new Column(children: <Widget>[
          Row(children: <Widget>[
            Expanded(
              child: Container(
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                    Text(
                      km + '公里',
                      style: TextStyle(fontSize: 18),
                    ),
                    Padding(
                      padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                      child: Text('表显里程', style: TextStyle(color: baseColor)),
                    )
                  ])),
              flex: 1,
            ),
            Expanded(
              child: Container(
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                    Text(
                      pl + 'T',
                      style: TextStyle(fontSize: 18),
                    ),
                    Padding(
                      padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                      child: Text('排量', style: TextStyle(color: baseColor)),
                    )
                  ])),
              flex: 1,
            ),
            Expanded(
              child: Container(
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                    Text(
                      cj,
                      style: TextStyle(fontSize: 18),
                    ),
                    Padding(
                      padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                      child: Text('车架号', style: TextStyle(color: baseColor)),
                    )
                  ])),
              flex: 1,
            )
          ]),
          Padding(
              padding: new EdgeInsets.fromLTRB(0, 15, 0, 5),
              child: Row(children: <Widget>[
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                        Text(
                          qd,
                          style: TextStyle(fontSize: 18),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child: Text('驱动', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                ),
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                        Text(
                          lx,
                          style: TextStyle(fontSize: 18),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child: Text('车型', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                ),
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                        Text(
                          pf,
                          style: TextStyle(fontSize: 18),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child:
                              Text('排放标准', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                )
              ])),
          Padding(
              padding: new EdgeInsets.fromLTRB(0, 15, 0, 5),
              child: Row(children: <Widget>[
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                        Text(
                          gc,
                          style: TextStyle(fontSize: 18),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child:
                              Text('国产/进口', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                ),
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                        Text(
                          fdj,
                          style: TextStyle(fontSize: 18),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child:
                              Text('发动机号', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                ),
                Expanded(
                  child: Container(
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                        Text(
                          cp,
                          style: TextStyle(fontSize: 14),
                        ),
                        Padding(
                          padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
                          child:
                              Text('车牌号', style: TextStyle(color: baseColor)),
                        )
                      ])),
                  flex: 1,
                )
              ])),
        ])));

    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(20, 5, 0, 25),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: tiles,
        ));
    return content;
  }

  //车辆详情
  Widget detail() {
    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(0, 15, 0, 5),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Row(
              children: <Widget>[
                new Container(
                    alignment: Alignment.topLeft,
                    width: 20,
                    child: new Image.asset(
                        'resource/images/zhanshi/cheliangguanli.png')),
                new Padding(
                    padding: new EdgeInsets.fromLTRB(5, 0, 0, 0),
                    child: Text('车辆详情')
                    // Row(
                    //   children: <Widget>[
                    //     Expanded(
                    //       flex: 1,
                    //       child: Text('上架时间'),
                    //     ),
                    //     Expanded(
                    //       flex: 1,
                    //       child: Text('2019.12.12'),
                    //     )
                    //   ],
                    // ),
                    )
              ],
            ),
          ],
        ));
    return content;
  }

  Widget detailInfo() {
    List<Widget> tiles = [];
    List carinfo =
        param["inspectorItemsResult"]["otherData"]["checkData"][0]["post"];
    for (int i = 0; i < param["car_thumb"].length; i++) {
      tiles.add(Padding(
        padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
        child: Image.network(param["car_thumb"][i], fit: BoxFit.fill),
      ));
    }
    // for (int i = 0; i < carinfo.length; i++) {
    //   var item = carinfo[i];
    //   tiles.add(Padding(
    //       padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
    //       child: Text(item["name"] + "：" + item["value"],
    //           style: new TextStyle(
    //               color: Color.fromRGBO(120, 120, 120, 1), fontSize: 13))));
    // }
    tiles.add(Padding(
        padding: new EdgeInsets.fromLTRB(0, 5, 0, 0),
        child: Text("车源描述" + "：" + param["car_descrble"],
            style: new TextStyle(
                color: Color.fromRGBO(120, 120, 120, 1), fontSize: 13))));

    Widget content = Padding(
        padding: new EdgeInsets.fromLTRB(20, 5, 0, 25),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: tiles,
        ));
    return content;
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(
          '车辆信息',
          style: TextStyle(color: Colors.white),
        ),
        backgroundColor: Color.fromRGBO(7, 169, 251, 1),
        leading: BackButton(color: Colors.white),
      ),
      body: new Center(
          child: Padding(
              padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
              child: Stack(children: <Widget>[
                Align(
                  alignment: Alignment.bottomCenter,
                  child: new Padding(
                      padding: new EdgeInsets.fromLTRB(10, 0, 20, 0),
                      child: new Material(
                        color: Color.fromRGBO(239, 239, 239, 1),
                        //INK可以实现装饰容器
                        child: new Ink(
                          //用ink圆角矩形
                          //color: Color.fromRGBO(239, 239, 239, 1),
                          decoration: new BoxDecoration(
                            //背景

                            color: Color.fromRGBO(255, 204, 48, 1),
                            //设置四周圆角 角度
                            borderRadius:
                                BorderRadius.all(Radius.circular(18.0)),
                            //设置四周边框
                            border: new Border.all(
                                width: 1,
                                color: Color.fromRGBO(255, 204, 48, 1)),
                          ),
                          child: new InkWell(

                              //圆角设置,给水波纹也设置同样的圆角
                              //如果这里不设置就会出现矩形的水波纹效果
                              borderRadius: new BorderRadius.circular(15.0),
                              //设置点击事件回调
                              onTap: () {
                                //submit();
                                call();
                              },
                              child: Container(
                                //设置 child 居中
                                alignment: Alignment(0, 0),
                                height: 36,
                                //color: Color.fromRGBO(239, 239, 239, 1),
                                width: double.infinity,
                                child: Text(
                                  "联    系",
                                  style: TextStyle(color: Colors.white),
                                ),
                              )),
                        ),
                      )),
                ),
                Align(
                    child: Padding(
                        padding: new EdgeInsets.fromLTRB(0, 0, 0, 50),
                        child: new ListView(
                          children: <Widget>[
                            flash(),
                            titles(),
                            inspect(),
                            distube(),
                            distubetime(),
                            car_base_info(),
                            car_base_info_detail(),
                            detail(),
                            detailInfo(),
                          ],
                        ))),
              ]))),
    );
  }

  call() async {
    String url = 'tel:' + param["contact_mobile"];

    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
}
